<template>
	<view>
		<u-navbar back-icon-color="#ffffff" title="商铺实名认证" title-color="#ffffff" :background="background"></u-navbar>
		<view class="content">
			<u-form :model="form" :rules="rules" ref="uForm" :errorType="errorType">
				<u-form-item :leftIconStyle="{ color: '#888', fontSize: '32rpx' }" label-width="150" :label-position="labelPosition" label="真实姓名" prop="realName">
					<u-input :border="border" placeholder="请输入真实姓名" v-model="form.realName" type="text"></u-input>
				</u-form-item>
				<u-form-item :rightIconStyle="{ color: '#888', fontSize: '32rpx' }" :label-position="labelPosition" label="身份证号" prop="identityNumber" label-width="150">
					<u-input :border="border" placeholder="请输入身份证号" v-model="form.identityNumber" type="number"></u-input>
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="店铺地址" prop="storeAddress" label-width="150">
					<u-input type="textarea" disabled :border="border" placeholder="请填写店铺详细地址" v-model="form.storeAddress" @click="chooseLocation()" />
				</u-form-item>
			<!-- 	<u-form-item :label-position="labelPosition" label="店铺定位" prop="location" label-width="150">
					<u-input :border="border" disabled placeholder="精度" v-model="form.longitude" type="text"></u-input>
					<u-input :border="border" disabled placeholder="维度" v-model="form.latitude" type="text"></u-input>
					<u-button slot="right" type="success" size="mini" @click="chooseLocation()">获取店铺位置</u-button>
				</u-form-item>
				<view class="">
					<view class="">请在店铺内获取店铺位置</view>
				</view> -->
				<!-- <u-form-item :label-position="labelPosition" label="上传图片" prop="businessLicense" label-width="150">
					<u-upload :action="action" max-count="5" auto-upload="true" width="160"></u-upload>
				</u-form-item> -->
			</u-form>
			<view class="button"><u-button type="success" @click="submit">提交认证信息</u-button></view>
			<view><u-toast ref="uToast" /></view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 文字提示
			errorType: ['message'],
			// 演示地址，请勿直接使用
			action: 'http://10.57.58.73:8001/api/qiNiuContent/file',
			background: {
				backgroundImage: 'linear-gradient(90deg, rgb(0,164,133), rgb(51,241,205))'
			},
			//
			userInfo: {},
			//
			rules: {
				realName: [
					{
						required: true,
						message: '请输入真实姓名',
						trigger: 'blur'
					}
				],
				identityNumber: [
					{
						required: true,
						message: '请填写身份证号',
						trigger: 'blur'
					}
				],
				storeAddress: [
					{
						required: true,
						message: '请输入店铺地址',
						trigger: 'blur'
					}
				]
				/* location: [
					{
						required: true,
						message: '请获取店铺定位',
						trigger: 'blur'
					}
				], */
				/* businessLicense: [
					{
						required: true,
						message: '请上传营业执照',
						trigger: 'blur'
					}
				] */
			},
			form: {
				userId: '',
				realName: '',
				identityNumber: '',
				storeAddress: '',
				location: '',
				latitude: '',
				longitude: '',
				businessLicense: ''
			},
			//
			latitude:'',
			longitude:'',
			//
			labelPosition: 'left',
			border: false
		};
	},
	onLoad(e) {
		try {
			const token = uni.getStorageSync('token');
			if (token) {
				this.userInfo = uni.getStorageSync('userInfo');
				if(!this.userInfo){
					//#ifdef APP-PLUS
					uni.reLaunch({
						url: '/pages/login/index'
					});
					//#endif
					//#ifdef MP-WEIXIN
					uni.reLaunch({
						url: '/pages/login/xcxLogin'
					});
					//#endif
				}
			} else {
				//#ifdef APP-PLUS
				uni.reLaunch({
					url: '/pages/login/index'
				});
				//#endif
				//#ifdef MP-WEIXIN
				uni.reLaunch({
					url: '/pages/login/xcxLogin'
				});
				//#endif
			}
		} catch (e) {
			// error
		}
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	methods: {
		//
		chooseLocation(){
			let _t = this;
			uni.chooseLocation({
			    success: function (res) {
			        console.log('位置名称：' + res.name);
			        console.log('详细地址：' + res.address);
			        console.log('纬度：' + res.latitude);
			        console.log('经度：' + res.longitude);
					_t.setStoreAddress(res.longitude+'',res.latitude+'',res.address);
			    }
			});
		},
		setStoreAddress(longitude,latitude,storeAddress){
			this.form.storeAddress = storeAddress;
			this.form.latitude = latitude;
			this.form.longitude = longitude;
			console.log(this.form)
		},
		//
		getLocation() {
			let _t = this;
			console.log('getLocation11');
			uni.getLocation({
			    type: 'wgs84',
			    success: function (res) {
					_t.setLocation(res.longitude,res.latitude);
			    }
			});
			console.log('getLocation222');
		},
		setLocation(longitude,latitude){
			this.form.latitude = latitude;
			this.form.longitude = longitude;
		},
		//
		submit() {
			this.$refs.uForm.validate(valid => {
				this.form.businessLicense = '';
				this.form.latitude = '';
				this.form.longitude = '';
				console.log(this.form);
				if (valid) {
					this.$u.api.realName(this.form).then(res => {
						console.log('realName');
						console.log(res);
						if (res.data != null) {
							this.$refs.uToast.show({
								title: '认证成功',
								type: 'success'
							});
							this.$u.route({
								type: 'back'
							});
						} else {
							this.$refs.uToast.show({
								title: '认证失败',
								type: 'error'
							});
						}
					});
					console.log('验证通过');
				} else {
					this.$refs.uToast.show({
						title: '验证失败',
						type: 'error'
					});
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.content {
	padding: 0 30rpx;
	.button {
		padding-top: 50rpx;
	}
}
</style>
